package com.labot.dao.hibernate;

import java.util.Date;
import java.util.List;

import javax.jws.WebService;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.labot.dao.AuditDao;
import com.labot.model.audit.Audit;
import com.labot.types.AuditEventType;
import com.labot.types.AuditType;
import com.labot.util.DateUtil;

/**
 * This class interacts with Hibernate session to save/delete and
 * retrieve Config objects.
 *
 * @author Ing. Enrique Barbeito
*/
@Repository("auditDao")
@WebService(serviceName = "AuditService", endpointInterface = "com.labot.service.AuditService")
public class AuditDaoHibernate extends GenericDaoHibernate<Audit, Long> implements AuditDao {

    /**
     * Constructor that sets the entity to User.class.
     */
    public AuditDaoHibernate() {
        super(Audit.class);
    }
    
    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<Audit> getAll() {
        Query qry = getSession().createQuery("from Audit a");
        return qry.list();
    }
    
    public List<Audit> getBy(AuditEventType event, AuditType type, String executionPlace, String userName, String raw, Date from, Date to) {
    	String eventString = null;
    	String typeString = null;
    	if(event != null) {
    		eventString = event.toString();
    	}
    	if(type != null) {
    		typeString = type.toString();
    	}
    	return this.getBy(eventString, typeString, executionPlace, userName, raw, from, to);
    }

    public List<Audit> getBy(String event, String type, String executionPlace, String userName, String raw, Date from, Date to) {
    	String query = "from Audit a where 1=1 ";
    	if (event != null) {
    		query = query + " and a.event LIKE '%" + event + "%' ";
    	}
    	if (type != null) {
    		query = query + " and a.auditType = '" + type + "' ";
    	}
    	if (executionPlace != null) {
    		query = query + " and a.executionPlace LIKE '%" + executionPlace + "%' ";
    	}
    	if (userName != null) {
    		query = query + " and a.userName LIKE '%" + userName + "%' ";
    	}
    	if (raw != null) {
    		query = query + " and a.raw LIKE '%" + raw + "%' ";
    	}
    	if (from != null) {
    		query = query + " and a.time >= '" + DateUtil.dateToString(from, DateUtil.SQL_FECHA_HORA) + "' ";
    	}
    	if (to != null) {
    		query = query + " and a.time <= '" + DateUtil.dateToString(to, DateUtil.SQL_FECHA_HORA) + "' ";
    	}
    	query = query + " order by a.time DESC ";
        Query qry = getSession().createQuery(query);
        return qry.setMaxResults(300).list();
    }
    
    /**
     * {@inheritDoc}
     */
    public Audit save(Audit a) {
        if (log.isDebugEnabled()) {
            log.debug("Audit's id: " + a.getId());
        }
        getSession().saveOrUpdate(a);
        return a;
    }

	@Override
	public Audit log(AuditEventType event, AuditType auditType,
			String executionPlace, String userName, String raw) {
		Audit audit = new Audit();
		audit.setAuditType(auditType);
		audit.setEvent(event);
		audit.setExecutionPlace(executionPlace);
		audit.setUserName(userName);
		
		if(raw != null) {
			if(raw.length()>508) {
				audit.setRaw(raw.substring(0,508)+"...");
			}else{				
				audit.setRaw(raw);
			}
		}
		
		return save(audit);
		
	}


}
